package com.army.study.provider;

import com.army.study.ResourceTable;
import com.army.study.utils.LogUtil;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class FileListProvider<T> extends BaseItemProvider {
    private final static String TAG = FileListProvider.class.getSimpleName();

    // 上下文
    private final Context mContext;
    // 文件列表
    private final List<T> mList;

    public FileListProvider(Context context, List<T> listBean) {
        mContext = context;
        mList = listBean;
    }


    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public T getItem(int i) {
        return (mList == null || mList.size() == 0) ? null : mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int idx, Component component, ComponentContainer componentContainer) {
        FileItemViewHolder holder;

        if (component == null) {
            component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_file_item, null, false);
        }

        holder = (FileItemViewHolder)mList.get(idx);
        if (holder == null) {
            LogUtil.error(TAG, "FileItemViewHolder[]" + idx + " is null");
            return component;
        }

        Image icon = (Image)component.findComponentById(ResourceTable.Id_imageIcon);
        icon.setPixelMap(holder.getIconId());

        Text name = (Text)component.findComponentById(ResourceTable.Id_filename);
        name.setText(holder.getFileName());
        Text devName = (Text)component.findComponentById(ResourceTable.Id_deviceinfo);
        devName.setText(mContext.getString(ResourceTable.String_file_source_label) + ": " + holder.getDeviceName());

        return component;
    }

    protected void onItemClick(Component component, T item, int position) {
    }

}
